package boerse.main.Account;

import boerse.main.Asset.CashAccount;
import boerse.main.Asset.Share;
import boerse.main.Asset.ShareDeposit;
import boerse.main.Exceptions.NegativeShareCountException;
import boerse.main.Exceptions.NegativeValueException;
import boerse.main.Exceptions.NoSuchPlayerException;
import boerse.main.Exceptions.NotEnoughMoneyException;
import boerse.main.StockPriceProvider.StockPriceInfo;
import boerse.main.Gui.DrawGraph;

public class AccountManagerImpl implements AccountManager {

		private Player[] allPlayer;
		private StockPriceInfo priceInfo;
		
        public AccountManagerImpl(StockPriceInfo priceInfo) {
            this.priceInfo = priceInfo;
        	allPlayer = new Player[10];
        }

		@Override
		public void createPlayer(String name) {
			CashAccount cashAccount = new CashAccount(name + "` Account");
			ShareDeposit shareDeposit = new ShareDeposit(name + "` Share Depo");
			
			Player newPlayer = new Player(name, cashAccount, shareDeposit);
			
			for (int i = 0; i < allPlayer.length; i++) {
				if (allPlayer[i] == null) {
					allPlayer[i] = newPlayer;
					return;
				}
			}		
			System.out.println("Playerlist is full.");
			
		}

		@Override
		public void buyShare(String playerName, String shareName, int count) throws NotEnoughMoneyException, NegativeValueException {
			Player player = searchPlayer(playerName);	
			Share searchedShare = priceInfo.getShare(shareName);
			
			player.getShareDeposit().buy(searchedShare, count, player.getCashAccount());
		}




		@Override
		public void sellShare(String playerName, String shareName, int count) throws NegativeValueException, NegativeShareCountException {
			Player player = searchPlayer(playerName);	
			Share searchedShare = priceInfo.getShare(shareName);
			
			player.getShareDeposit().sell(searchedShare, count, player.getCashAccount());	
				
				
		}

		@Override
		public long getCashAccountPrice(String playerName) {					
			return searchPlayer(playerName).getCashAccount().determinePrice();
		}
		
		@Override
		public long getShareDepositPrice(String playerName) {
			return searchPlayer(playerName).getShareDeposit().determinePrice();
		}

		@Override
		public long getAllAssets(String playername) {
			
			Player player = searchPlayer(playername);
			long price = 0;
				
			price = player.getCashAccount().determinePrice() + player.getShareDeposit().determinePrice();
				
			return price;
		
		}


		
		@Override
		public void addMoney(String playername, int money) throws NegativeValueException {
				searchPlayer(playername).getCashAccount().addMoney(money);			
		}
		
		
		private Player searchPlayer(String playerName) {
			
			for (int i = 0; i < allPlayer.length; i++) {
                if (allPlayer[i] != null) {
                    if (allPlayer[i].getName().equals(playerName)) {
                        return allPlayer[i];
                    }
                }
			}
			
			throw new NoSuchPlayerException();
		}


    public void showTicker() {
        priceInfo.showTicker();
    }



    public void drawGraph(String shareName) {
        Share searchedShare = priceInfo.getShare(shareName);
        DrawGraph myFrame = new DrawGraph(searchedShare,priceInfo.getTime());

        myFrame.start(myFrame, searchedShare, priceInfo.getTime());
    }

//    public void drawGraph(String shareName) {
//        Share searchedShare = priceInfo.getShare(shareName);
//        DrawGraphb draw = new DrawGraphb(searchedShare);
//
//    }

}
